package com.sywb.chuangyebao.view.dialog;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.CommentReply;
import com.sywb.chuangyebao.contract.CommentReplyContract;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ScreenUtils;
import org.bining.footstone.utils.SoftKeyboardUtils;
import org.bining.footstone.utils.ToastUtils;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentReplyDialog extends BaseDialogFragment<CommentReplyContract.Presenter> implements CommentReplyContract.BaseView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.common_recycler_layout)
    RelativeLayout commonRecyclerLayout;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_send)
    TextView tvSend;

    private int contentHeight; // DialogActivity高度
    private String commentJson;
    private int postion;
    private String source;
    private CommentReply commentInfo;
    private String replyType;
    private int replyId;
    private int toUid;

    public static CommentReplyDialog newInstance(Object... pramars) {
        CommentReplyDialog dialogFragment = new CommentReplyDialog();
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_comment_reply;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exit();
            }
        });

        if (bundle != null) {
            contentHeight = bundle.getInt("p0", 0);
            commentJson = bundle.getString("p1");
            postion = bundle.getInt("p2", 0);
            source = bundle.getString("p3");
        } else {
            contentHeight = getArguments().getInt("p0", 0);
            commentJson = getArguments().getString("p1");
            postion = getArguments().getInt("p2", 0);
            source = getArguments().getString("p3");
        }

        Logger.e("commentJson:" + commentJson);

        commentInfo = JSON.parseObject(commentJson, CommentReply.class);

        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

//        etContent.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    if (!Utils.checkIsNext(getMyFragmentManager())) {
//                        etContent.clearFocus();
//                        SoftKeyboardUtils.stopSoftKeybord(etContent);
//                        return;
//                    }
//                    // 此处为得到焦点时的处理内容
//                } else {
//                    // 此处为失去焦点时的处理内容
//                }
//            }
//        });

    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @OnClick({R.id.et_content, R.id.tv_send})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.et_content:

                if (!Utils.checkIsNext(getMyFragmentManager())) {
//                    etContent.clearFocus();
//                    SoftKeyboardUtils.stopSoftKeybord(etContent);
                    return;
                }

                if (isSoftShowing()) {
                    setReplyName("comment", commentInfo.comment_id, commentInfo.from_uid, "写回复...");
                }
                break;
            case R.id.tv_send:
                String content = etContent.getText().toString();
                if (content.isEmpty()) {
                    ToastUtils.show(mActivity, "请输入评论内容");
                } else {
                    mPresenter.addCommentReply(replyType, replyId, toUid, content);
                    etContent.setText("");
                    etContent.setHint("写回复...");

                    SoftKeyboardUtils.stopSoftKeybord(etContent);
                    // setWindowHeight(true);

                    this.replyType = "comment";
                    this.replyId = commentInfo.comment_id;
                    this.toUid = commentInfo.from_uid;
                }
                break;
        }
    }

    private boolean isSoftShowing() {
        //获取当前屏幕内容的高度
        int screenHeight = ScreenUtils.getScreenHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        getDialog().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

        return screenHeight - rect.bottom - ScreenUtils.getNavigationBarHeight() != 0;
    }

    private void setWindowHeight(boolean isAppoint) {
        WindowManager.LayoutParams p = getDialog().getWindow().getAttributes();
        p.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        p.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
//        if (!source.equals("article")) {
//            if (contentHeight > 0 && isAppoint) {
//                p.height = contentHeight;
//            } else {
//                p.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
//            }
//        } else {
//            p.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
//        }

        // p.gravity = Gravity.BOTTOM;
        // p.dimAmount = 0.8f;
        getDialog().getWindow().setAttributes(p);
    }

    @Override
    public void onResume() {
        super.onResume();
        setWindowHeight(true);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
    }

    @Override
    public CommentReply getComment() {
        return commentInfo;
    }

    @Override
    public void setCommentCount(int commentCount) {
        tvTitle.setText(commentCount + "条回复");
        if (postion >= 0) {
            RxBus.get().post(Constants.HTTP_COMMENTREPLY_ADD, source + "&" + postion + "&" + commentCount);
        }
        RxBus.get().post(Constants.COMMENT_REPLY_CHANGE, "");
    }

    @Override
    public void setCommentThumb(boolean isThumb, int thumbCount) {
        if (postion >= 0) {
            RxBus.get().post(Constants.POST_THUMB, source + "&" + postion + "&" + isThumb + "&" + thumbCount);
        }
    }

    @Override
    public void setReplyName(String replyType, int replyId, int toUid, String name) {
        this.replyType = replyType;
        this.replyId = replyId;
        this.toUid = toUid;

        etContent.setHint(name);

        etContent.setFocusable(true);
        etContent.setFocusableInTouchMode(true);
        etContent.requestFocus();
        etContent.requestFocusFromTouch();

        // setWindowHeight(false);
        SoftKeyboardUtils.openSoftKeybord(etContent);
    }

}
